package xyz.wanwanwan.www.hrm.service.impl;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import xyz.wanwanwan.www.hrm.CourseDoc;
import xyz.wanwanwan.www.hrm.query.CourseQuery;
import xyz.wanwanwan.www.hrm.repository.CourseElasticsearchRepository;
import xyz.wanwanwan.www.hrm.service.ISearchCourse;
import xyz.wanwanwan.www.hrm.util.PageList;

@Service
public class SearchCourseImpl implements ISearchCourse {
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;
    @Override
    public PageList<CourseDoc> queryCourse(CourseQuery query) {
        //获取本地查询器
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
       //分页
        builder.withPageable(PageRequest.of(query.getPage()-1,query.getRows()));
        String sortFields = query.getSortField();
        String sortField = null;
        if(StringUtils.isNotEmpty(sortFields)){
            //按照条件排序
            switch (sortFields){
                case "xl": sortField = "saleCount" ;break;
                case "xp": sortField = "onlineTime"; break;
                case "pl": sortField = "commentCount"; break;
                case "jg": sortField = "price";break;
                case "rq": sortField = "viewCount";break;
            }
            String sortType = query.getSortType();
            SortOrder sortOrder = SortOrder.DESC;
            if(StringUtils.isNotEmpty(sortType)&&sortType.toLowerCase().equals("desc")){
                sortOrder =SortOrder.ASC;
            }
            builder.withSort(new FieldSortBuilder(sortField).order(sortOrder));
        }
        //获取组合查询器
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //关键字模糊查询
        if(!StringUtils.isEmpty(query.getKeyword())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("myField",query.getKeyword()));
        }
        //课程类型查询
        if(null!=query.getProductType()){
            boolQueryBuilder.filter(QueryBuilders.termQuery("courseTypeId",query.getProductType()));
        }
        //价格搜索
        if(null!=query.getPriceMin()){
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(query.getPriceMin()));
        }
        if(null!=query.getPriceMax()){
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").lte(query.getPriceMax()));
        }
        builder.withQuery(boolQueryBuilder);
        //获取
        Page<CourseDoc> searchCourses = courseElasticsearchRepository.search(builder.build());
        //转换为pageList并返回
        return new PageList<>(searchCourses.getTotalElements(),searchCourses.getContent());
    }
}
